package loader;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import postmortem.Tracing;

public class loader {
	static Thread thread;
	public static LoaderWindow lw;
	public loader(Thread thr) {
		thread = thr;
	}
	
	public static void main(String[] args){
		lw = new LoaderWindow();
		lw.setVisible(true);
	}

}

class LoaderWindow extends JFrame {
	JPanel mainPanel = new JPanel();
	
	JPanel chkPanel = new JPanel();
	JPanel cmbPanel = new JPanel();
	JPanel btnPanel = new JPanel();
	JPanel lblPanel = new JPanel();
	
	JCheckBox chkBox = new JCheckBox("Enabled");
	String[] cmbOptions = {"Select Inspection Mode", "Postmortem", "Live"};
	JComboBox cmbBox = new JComboBox(cmbOptions);
	JButton btnInspect = new JButton("Start Target Program");
	JLabel lbl = new JLabel("<html><font color='red'>Status: Tracing Disabled</font></html>");

	
	Boolean chkBoxBoolean = false;
	String cmbBoxString = "";
	
	LoaderWindow() {
		
		this.setTitle("Java UI-Inspector");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setPreferredSize(new Dimension (200, 200));
		this.setResizable(false);
		Container pane = this.getContentPane();
		cmbBox.setEnabled(false);
		
		chkBox.addItemListener(new ItemListener() {

			public void itemStateChanged(ItemEvent arg0) {
				if (chkBox.isSelected()){
					cmbBox.setEnabled(true);
					chkBoxBoolean = true;
					lbl.setText("<html><font color='green'>Status: Tracing Enabled</font></html>");
				}
				else {
					cmbBox.setEnabled(false);
					chkBoxBoolean = false;
					
					lbl.setText("<html><font color='red'>Status: Tracing Disabled</font></html>");
				}
			}
		});
		
		chkPanel.add(chkBox);
		
		cmbBox.addItemListener(new ItemListener() {

			
			public void itemStateChanged(ItemEvent e) {
				cmbBoxString = (String) cmbBox.getSelectedItem();
			}
			
		});
		
		cmbPanel.add(cmbBox);
		btnInspect.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if (cmbBox.getSelectedItem().equals("Select Inspection Mode") && chkBox.isSelected()){
					JOptionPane.showMessageDialog(loader.lw, "Please select an inspection mode");
				}
				else {
				
				loader.lw.setVisible(false);
				if (cmbBoxString.equals("Postmortem")){
					postmortem.Tracing.stop = false; //this is on
					live.Aspect.enableLive = false; // this is off
					
				}
					
				else if (cmbBoxString.equals("Live")){
					postmortem.Tracing.stop = true; // this is off
					live.Aspect.enableLive = true; //this is on
				}
					
				loader.thread.resume();
				
			}
			}
			
		});
		
		btnPanel.add(btnInspect);
		lblPanel.add(lbl);
		
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		mainPanel.add(chkPanel, BorderLayout.NORTH);
		mainPanel.add(cmbPanel, BorderLayout.CENTER);
		mainPanel.add(btnPanel, BorderLayout.SOUTH);
		mainPanel.add(lblPanel, BorderLayout.SOUTH);
		
		pane.add(mainPanel);
		this.setLocation(new Point(0, 0));
		this.pack();
	}
}



